<?php
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

?><form method="post">
<textarea name="submit" rows="4" cols="100"><?php 
if (!empty($_POST['submit'])) echo htmlspecialchars($_POST['submit']);
?></textarea><br>
<input type="submit">
<?php
$mpps = array();

if (!empty($_POST['submit'])) {
	preg_match_all('|0x([0-5]+)|i', $_POST['submit'], $matched);
	foreach ($matched[1] as $match) $mpps[] = strrev(str_pad($match, 8, '0'));
	$mpps = preg_split('//', implode($mpps), -1, PREG_SPLIT_NO_EMPTY);
}

/* All possible MPPs */
define ('MPP_UNUSED', 'MPP_UNUSED');
define ('MPP_GPIO', 'MPP_GPIO');
define ('MPP_PCIE_RST_OUTn', 'MPP_PCIE_RST_OUTn');
define ('MPP_PCI_ARB', 'MPP_PCI_ARB');
define ('MPP_PCI_PMEn', 'MPP_PCI_PMEn');
define ('MPP_GIGE', 'MPP_GIGE');
define ('MPP_NAND', 'MPP_NAND');
define ('MPP_PCI_CLK', 'MPP_PCI_CLK');
define ('MPP_SATA_LED', 'MPP_SATA_LED');
define ('MPP_UART', 'MPP_UART');

// Source of table: http://nas-central.org/index.php/Orion_NAS_customisation_guide
$mpptable = array();
$mpptable[0] = array(MPP_PCIE_RST_OUTn, MPP_UNUSED, MPP_PCI_ARB, MPP_GPIO);
$mpptable[1] = array(MPP_GPIO, MPP_UNUSED, MPP_PCI_ARB);
$mpptable[3] = $mpptable[1];

$mpptable[2] = array(MPP_GPIO, MPP_UNUSED, MPP_PCI_ARB, MPP_PCI_PMEn);
$mpptable[4] = array(MPP_GPIO, MPP_UNUSED, MPP_PCI_ARB, MPP_UNUSED, MPP_NAND, MPP_SATA_LED);
$mpptable[5] = $mpptable[6] = $mpptable[7] = $mpptable[4];

$mpptable[8] = array(MPP_GPIO, MPP_GIGE);
$mpptable[11] = $mpptable[10] = $mpptable[9] = $mpptable[8];

$mpptable[12] = array(MPP_GPIO, MPP_GIGE, MPP_UNUSED, MPP_UNUSED, MPP_NAND, MPP_SATA_LED);
$mpptable[15] = $mpptable[14] = $mpptable[13] = $mpptable[12];
$mpptable[16] = array(MPP_UART,	MPP_GIGE, MPP_UNUSED, MPP_UNUSED, MPP_NAND, MPP_GPIO);
$mpptable[17] = $mpptable[16];

$mpptable[18] = array(MPP_UART, MPP_GIGE, MPP_UNUSED, MPP_UNUSED, MPP_UNUSED, MPP_GPIO);
$mpptable[19] = $mpptable[18];

if ($mpps) {
	echo '<pre>';
	echo "static struct orion5x_mpp_mode board_mpp_modes[] __initdata = {\n";
	foreach ($mpps as $mpp_id => $mpp) {
		if (!isset($mpptable[$mpp_id])) break;
		if (!isset($mpptable[$mpp_id][$mpp])) $pin = MPP_UNUSED;
		else $pin = $mpptable[$mpp_id][$mpp];
		echo "\t{ $mpp_id, $pin },\n";
	}
	echo "\t{ -1 },\n};";
	echo '<pre>';
}

?>
<p>Sample input:</p><pre>
#define BUFFALO_BOARD_MPP0_7	0x11223344
#define BUFFALO_BOARD_MPP8_15	0x55112200
#define BUFFALO_BOARD_MPP16_23	0x11005511
</pre>